package com.lumi.max.jnicall.serialoperate;

import android.content.Context;

/**
 * Created by LME09 on 2016/3/18.
 */
public abstract class SerialReadWrite {

    public interface SerialReadWriteMethod {
        void iniDevice(Context context);
        int getDevCount();
        boolean isDeviceOpen();
        void setConfig(int baudRate, byte dataBit, byte stopBit, byte parity, byte flowControl);
//        void writeData(String string);
        void onSendDataToDongle(String string);
        void enableRead(boolean flag);
        void connectFunction(Context context);
        void disconnectFunction();
        boolean getUartConifged();
    }

    public abstract void iniDevice(Context context);

    public abstract boolean getUartConfiged();

    public abstract int getDevCount();

    public abstract boolean isDeviceOpen();

    public abstract void setConfig(int baudRate, byte dataBit, byte stopBit, byte parity, byte flowControl);

    public abstract void writeData(byte[] data);

//    public abstract int readTask(char[] readData);
    public abstract String readTask();

    public static final String toHex(byte[] data, int off, int length) {
        // double size, two bytes (hex range) for one byte
        StringBuffer buf = new StringBuffer(data.length * 2);
        for (int i = off; i < length; i++) {
            // don't forget the second hex digit
            if (((int) data[i] & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toString((int) data[i] & 0xff, 16));
            if (i < data.length - 1) {
                buf.append(" ");
            }
        }
        return buf.toString();
    }

//    public  String hexStr2Str(String hexStr) {
//        String str = "0123456789ABCDEF";
//        char[] hexs = hexStr.toCharArray();
//        byte[] bytes = new byte[hexStr.length() / 2];
//        int n;
//        for (int i = 0; i < bytes.length; i++) {
//            n = str.indexOf(hexs[2 * i]) * 16;
//            n += str.indexOf(hexs[2 * i + 1]);
//            bytes[i] = (byte) (n & 0xff);
//        }
//        return new String(bytes);
//    }
//    */
//    public static String byte2HexStr(byte[] b) {
//        String hs = "";
//        String stmp = "";
//        for (int n = 0; n < b.length; n++) {
//            stmp = (Integer.toHexString(b[n] & 0XFF));
//            if (stmp.length() == 1)
//                hs = hs + "0" + stmp;
//            else
//                hs = hs + stmp;
//            // if (n<b.length-1) hs=hs+":";
//        }
//        return hs.toUpperCase();
//    }

    public abstract void enableRead(boolean flag);

    public abstract void connectFunction(Context context);

    public abstract void disconnectFunction();

    public static SerialReadWrite create(String type) {
        switch (type) {
            case "PL2303":
                return new PL2303Class();
            case "TN147":
                return new TN147Class();
        }
        return null;
    }

}
